package com.snowfield.lbcommon.utils.mail;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailException;
import cn.hutool.extra.mail.MailUtil;
import com.sun.mail.smtp.SMTPSenderFailedException;
import com.sun.mail.util.MailConnectException;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MailUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(MailUtils.class);

    /**
     * 发送邮件
     *
     * @param mailConfig
     * @param tos
     * @param subject
     * @param content
     * @param isHtml
     * @return
     */
    public static Integer send(HashMap<String, String> mailConfig, List<String> tos, String subject, String content, Boolean isHtml) {
        return send(mailConfig, tos, subject, content, isHtml, null);
    }

    /**
     * 发送邮件
     *
     * @param mailConfig
     * @param tos
     * @param subject
     * @param content
     * @param isHtml
     * @param attacheFiles
     * @return
     */
    public static Integer send(HashMap<String, String> mailConfig, List<String> tos, String subject, String content, Boolean isHtml, File... attacheFiles) {
        MailAccount account = new MailAccount();
        account.setHost(mailConfig.get("host"));
        account.setPort(NumberUtils.toInt(mailConfig.get("port"), 25));
        account.setAuth(Boolean.valueOf(mailConfig.get("auth")));
        account.setFrom(mailConfig.get("from"));
        account.setUser(mailConfig.get("user"));
        account.setPass(mailConfig.get("pass"));

        try {
            String result = MailUtil.send(account, tos, subject, content, isHtml, attacheFiles);

            LOGGER.info(result);
        } catch (MailException e) {
            Throwable cause = e.getCause();
//            LOGGER.error("throwables ", cause);
            if (cause instanceof MailConnectException) {
                //网络异常或服务器地址无效，请检查服务器地址、端口是否正确以及网络是否可达
                LOGGER.error("网络异常或服务器地址无效，请检查服务器地址、端口是否正确以及网络是否可达");
                return -1;
            } else if (cause instanceof AuthenticationFailedException) {
                //认证失败,用户名或密码错误,请检查用户名或密码是否正确
                LOGGER.error("认证失败,用户名或密码错误,请检查用户名或密码是否正确");
                return -2;
            } else if (cause instanceof SendFailedException) {
                cause = cause.getCause();
                if (cause instanceof SMTPSenderFailedException) {
                    //发送邮箱地址无效，请检查发送邮箱地址是否正确
                    LOGGER.error("发送邮箱地址无效，请检查发送邮箱地址是否正确");
                    return -3;
                } else if (cause == null) {
                    //接收邮箱地址无效，请检查接收邮箱地址是否正确
                    LOGGER.error("接收邮箱地址无效，请检查接收邮箱地址是否正确");
                    return -4;
                }
            } else if (cause instanceof MessagingException) {
                cause = cause.getCause();
                if (cause instanceof FileNotFoundException) {
                    //构造邮件信息失败，可能是找不到附件文件
                    LOGGER.error("构造邮件信息失败，请检查附件文件是否存在");
                    return -5;
                }
            }
            return 0;
        } catch (Exception e) {
            //其它未知错误
            LOGGER.error("", e);
            return 0;
        }
        return 1;
    }

}
